#     Xepec.ru - социальная сеть
#     Copyright (C) 2008  Fedorchenko Alexander <admin@xepec.ru>
#
#     This program is free software: you can redistribute it and/or modify
#     it under the terms of the GNU Affero General Public License as
#     published by the Free Software Foundation, either version 3 of the
#     License, or (at your option) any later version.
#
#     This program is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU Affero General Public License for more details.
#
#     You should have received a copy of the GNU Affero General Public License
#     along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#     Autor: Fedorchenko Alexander
#     Mail: <admin@xepec.ru>
#     Date: 06/02/2010
#

#$num = 3;
#while($num--){
#    sleep(1);
#}

require "../core.pl";
#require "../config.pl";

require "lib_search.pl";

&get_query();

my $auth_id = do_check();

my $act = $queryString{"act"};
my $cat = $queryString{"cat"};
#my $query = $queryString{"s"};
#$query =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
#$query =~ s/!/'/g;

if($queryString{"sort"}){$sort= "ORDER by ".$queryString{'sort'}." DESC"}else{$sort="ORDER by pepyaka DESC";}

my %search_param;
if($queryString{"email"}){$search_param{"email"} = "LIKE '%".$queryString{"email"}."%'";}
if($queryString{"nic"}){$search_param{"nic"} = "LIKE '%".$queryString{'nic'}."%'";}
if($queryString{"name"}){$search_param{"name"} = "LIKE '%".$queryString{'name'}."%'";}
if($queryString{"sex"}){$search_param{"sex"} = "='".$queryString{'sex'}."'";}

if($queryString{"age"})
{
	$sravn = $queryString{"age_t"};
	if(!$sravn)
	{
		$sravn = "=";
	}
	else
	{
		if($sravn eq "b"){$sravn = ">";}
		if($sravn eq "m"){$sravn = "<";}
	}
	$search_param{"bday!=0 AND (YEAR(CURRENT_DATE)-YEAR(bday))-(RIGHT(CURRENT_DATE,5)<RIGHT(bday,14))"} = $sravn.$queryString{"age"}."";
}
#$search_param{"phone_country"} = $cgi->param('phone_country'); $search_param{"phone_city"} = $cgi->param('phone_city'); $search_param{"phone_number"} =$cgi->param('phone_number');
#$search_param{"phone"} = $phone;
if($queryString{"foto"}){$search_param{"foto"} = "=".$queryString{'foto'};}
#$search_param{"name1"} = $cgi->param('name');
#$search_param{"name"} = $cgi->param('name');
my $query;

	while ( ($key,$value) = each %search_param )
	{
		if($value ne "")
		{
			#$value =~ s/\+/ /g;
			if($query eq '')
			{
				$query = "$key $value";
			}
			else
			{
				$query = "$query AND $key $value";
			}
		}
	}

log_write("$query\n");
#####################################################-Разбивка на страницы-####################################################

if($queryString{"page"})
{
	$page = $queryString{'page'};
	$start_item = $page * $SIPP - $SIPP;

}
else
{
	$start_item = "0";
}
	$limit = "LIMIT $start_item,$SIPP";
#####################################################-Разбивка на страницы-####################################################

###############################################################################################################################
#------------------------------------------------------Вывод  информации------------------------------------------------------#
###############################################################################################################################

print "Content-type: text/xml; charset=utf-8\n";
print "Expires: Mon, 26 Jul 1997 05:00:00 GMT\n";
#print "Last-Modified: ".date("D, d M Y H:i:s")." GMT\n";
print "Cache-Control: no-store, no-cache, must-revalidate\n";
print "Cache-Control: post-check=0, pre-check=0\n";
print "Pragma: no-cache\n\n";

	if ($query)
	{
	print qq{<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE search-new [
	<!ELEMENT search-new (query, result)>
	<!ELEMENT query (#PCDATA)>

	<!ELEMENT result (item*)>
		<!ATTLIST result 
		start_position CDATA "1"
		items CDATA "1"
		pages CDATA "1"
		error_code CDATA "0">
	<!ELEMENT item EMPTY>
	<!ATTLIST item 
	id CDATA #REQUIRED
	name CDATA #REQUIRED
	foto (0|1) "0"
	nic CDATA #REQUIRED
	status CDATA #REQUIRED
	sex (0|1|8) "0"
	pepyaka CDATA "0"
	age CDATA "0">
]>
};
		print qq{\n\t\t<search-new>};
			my $numrows = $dbcnx->selectrow_array(qq{SELECT COUNT(*) FROM username WHERE $query});
			
			use POSIX;
			$pages_count = ceil($numrows / $SIPP);
			$start_position	= $start_item + 1;

			if ($numrows == 0){$error="1";}
			elsif($page > $pages_count){$error="2";}
			else{$error="0";}
			
			print qq{\n\t\t\t<query>query</query>};
			print qq{\n\t\t\t<result start_position="$start_position" items="$numrows" pages="$pages_count" error_code="$error">\n};			
			
			$ath = $dbcnx->prepare(qq{SELECT *,(YEAR(CURRENT_DATE) - YEAR(bday)) - (RIGHT(CURRENT_DATE,5)<RIGHT(bday,14)) AS age FROM username WHERE $query $sort $limit});
			$ath->execute() or die("hi (error #3)");
			#@sss = $ath->fetchrow_array();

			while(my @sss = $ath->fetchrow_array() )
			{
				print qq{\t\t\t\t<item id="@sss['0']" name="@sss['1']" foto="@sss['2']" nic="@sss['3']" status="@sss['13']" sex="@sss['5']" pepyaka="@sss['11']" age="@sss['17']" />\n};
			}

			print qq{\t\t\t</result>};
		print qq{\n\t\t</search-new>};
	}
